package com.example.jdai.jdai.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jdai.jdai.R;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hellopyj on 2017/1/6.
 */

public class BottomBar extends RelativeLayout {
    @BindView(R.id.bt_check)
    Button btCheck;
    @BindView(R.id.tv_check)
    TextView tvCheck;
    @BindView(R.id.bt_my)
    Button btMy;
    @BindView(R.id.tv_my)
    TextView tvMy;
    int currect = R.id.bt_check;
    int currecttext = R.id.tv_check;
    @BindColor(R.color.mainColor)
    int orange;
    @BindColor(R.color.bottom_gray)
    int gray;
    BottomBarClick bottomBarClick;
    @BindView(R.id.ll_check)
    LinearLayout llCheck;
    @BindView(R.id.ll_my)
    LinearLayout llMy;

    public BottomBar(Context context) {
        super(context);
        init(context, null);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs) {
        //TypedArray ts = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
//        String title = ts.getString(R.styleable.TopBar_titleName);
//        String right_text = ts.getString(R.styleable.TopBar_righttext);
//        int vi = ts.getInt(R.styleable.TopBar_rightvisable, 0);
        LayoutInflater.from(context).inflate(R.layout.bottom_bar, this);
        ButterKnife.bind(this);
        btCheck.setEnabled(false);
        ((TextView) findViewById(currecttext)).setTextColor(orange);

    }

    @OnClick({R.id.ll_check,R.id.bt_check, R.id.ll_my,R.id.bt_my})
    public void onClick(View view) {
        int id = view.getId();
        if (currect != id)
            switch (id) {
                case R.id.ll_check:
                    case R.id.bt_check:
                    setSelect(R.id.bt_check);
                    setTextC(R.id.tv_check);
                    break;
                    case R.id.ll_my:
                case R.id.bt_my:
                    setSelect(R.id.bt_my);
                    setTextC(R.id.tv_my);
                    break;
            }
    }

    void setSelect(int id) {
        if (bottomBarClick != null)
        bottomBarClick.click(id);
        findViewById(currect).setEnabled(true);
        currect = id;
        findViewById(currect).setEnabled(false);
    }

    void setTextC(int id) {
        if (currecttext != id) {
            ((TextView) findViewById(currecttext)).setTextColor(gray);
            currecttext = id;
            ((TextView) findViewById(currecttext)).setTextColor(orange);
        }

    }

    public interface BottomBarClick {
        void click(int id);
    }

    public void setBottomBarClick(BottomBarClick bottomBarClick) {
        this.bottomBarClick = bottomBarClick;
    }
}
